package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.model.Category;
import com.huahang.fashionpress.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category")
public class AdminCategoryController {
    @Resource
    CategoryService categoryService;
    //显示所有商品类别
    @RequestMapping("/list")
    public ModelAndView linkCategory(Map<String,Object> map)
    {
        List<Category> categoryList = categoryService.SelectallCategory();
        if(categoryList == null&&categoryList.size()==0){

        }
        map.put("categoryList",categoryList);
        return  new ModelAndView("admin/category/list",map);
    }
    //根据ID查询商品类别
    @RequestMapping(value ="/selectById/{id}")
    public ModelAndView SelectCategoryById(Map<String,Object> map,@PathVariable int id){
        Category category  = categoryService.SelectCategoryById(id);
        List<Category> categoryList = new ArrayList<>();
        categoryList.add(category);
        map.put("categoryList",categoryList);
        return new ModelAndView("admin/category/list",map);
    }
    //根据Name查询商品类别
    @RequestMapping(value ="/selectByName/{categoryName}")
    public ModelAndView SelectCategoryById(Map<String,Object> map,@PathVariable String categoryName){
        Category category  = categoryService.SelectCategoryByName(categoryName);
        List<Category> categoryList = new ArrayList<>();
        categoryList.add(category);
        map.put("categoryList",categoryList);
        return new ModelAndView("admin/category/list",map);
    }
    //打开添加商品类别页面
    @RequestMapping(value = "/addpage")
    public String linkaddCategory()
    {
        return "admin/category/add";
    }
    //往数据库中添加商品类别
//    @RequestMapping(value = "/addcategory",method = RequestMethod.POST)
//    public String addCategory(String categoryName, String categoryDescribe, Model model)
//    {
//        Category category =new Category();
//        category.setCategoryName(categoryName);
//        category.setCategoryDescribe(categoryDescribe);
//
//    }
    //获取需要更新的商品类别
    @RequestMapping(value = "update",method = RequestMethod.GET)
    public ModelAndView updateCategory(int id,Map<String,Object> map)
    {
        Category category = categoryService.SelectCategoryById(id);
        map.put("category",category);
        return new ModelAndView("admin/category/add",map);
    }
    //传出更改后的商品类别
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String updateCategory(Category category,Model model)
    {
        if(category.getCategoryId()==null){
            categoryService.insertCategory(category);
            model.addAttribute("msg", "添加商品分类成功！");
        }
        else {
            categoryService.updateCategory(category);
            model.addAttribute("msg", "修改商品分类成功！");
        }

        model.addAttribute("url", "/fashionpress/category/list");
        return "admin/common/success";
    }
    @RequestMapping(value = "delete")
    public String deleteCategory(int id,Model model){
        categoryService.delteCategory(id);
        model.addAttribute("msg", "修改商品分类成功！");
        model.addAttribute("url", "/fashionpress/category/list");
        return "admin/common/success";
    }
}
